/*
 * Copyright (C) 2011, Pavel Samko <bulldozerbsg@gmail.com>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the "Pavel Samko" nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

#include "VCClient.h"

VCIClient * VCNewClient()
{
	VCClient * result = new VCClient();
	if (result && !result->Init())
	{
		delete result;
		result = NULL;
	}
	return result;
}

void VCDeleteClient(VCIClient * client)
{
	if (client)
	{
		delete client;
	}
}

VCClientController::VCClientController(VCClient & client)
: VCIController() 
, m_init_notify_count_ok(0)
, m_init_notify_count_false(0)
, m_micon(false)
, m_client(client)
{

}

VCClientController::~VCClientController()
{
	delete m_in;
	delete m_out;
	delete m_enc;
	delete m_dec;
	delete m_aec;
	delete m_net;
}

void VCClientController::OnNotifyFromMod(VCIMod & from, VC_NOTIFY notify)
{
	switch (notify)
	{
	case VC_INFO_INIT:
		{
			VCAutoMutex lock(m_init_notify_counts_mutex);
			m_init_notify_count_ok++;
		}
		break;
	case VC_ERROR_INIT:
		{
			VCAutoMutex lock(m_init_notify_counts_mutex);
			m_init_notify_count_false++;
		}
		break;
	default:
		{
		}
		break;
	}
// net
	VCModNetClient * net = dynamic_cast<VCModNetClient *>(&from);
	if (net && (notify == (VC_NOTIFY)VC_DISCONECT))
	{
		if (m_in && m_micon)
		{
			m_in->Cmd(VC_STOP);
			m_micon = false;
		}
		// not call m_client.Disconect() here
	}
}

bool VCClientController::Init()
{
#if VC_PLATFORM_WIN && VC_USE_WAVE_IN
	m_in = new VCModWaveIn(*this);
#else
	m_in = new VCModOpenALIn(*this);
#endif
#if VC_PLATFORM_WIN && VC_USE_WAVE_OUT
	m_out = new VCModWaveOut(*this);
#else
	m_out = new VCModOpenALOut(*this);
#endif
	m_enc = new VCModSpeexEnc(*this);
	m_dec = new VCModSpeexDec(*this);
	m_aec = new VCModSpeexAEC(*this);
	m_net = new VCModNetClient(*this);

	m_in->SetReceiver(m_aec);
	m_aec->SetReceiver(m_enc);
	m_enc->SetReceiver(m_net);
	m_net->SetReceiver(m_dec);
	m_dec->SetReceiver(m_aec->GetBackMod());
	m_aec->GetBackMod()->SetReceiver(m_out);

	while (true)
	{
		VCAutoMutex lock(m_init_notify_counts_mutex);
		if (m_init_notify_count_ok + m_init_notify_count_false >= VCCLIENTCONTROLLER_MODULES_COUNT)
			break;
		VCSleep(1);
	}
	if (m_init_notify_count_false > 0)
	{
		return false;
	}
	return true;
}

VCClient::VCClient()
{
	m_cli = new VCClientController(*this);
}

VCClient::~VCClient()
{
	if (IsConnected())
	{
		Disconnect();
	}
	if (m_cli)
	{
		delete m_cli;
	}
}

bool VCClient::Connect(const char * to_ip, unsigned short to_port)
{
	VCAutoMutex lock(m_cli_mutex);
	if (m_cli && m_cli->m_net)
	{
		return m_cli->m_net->Connect(to_ip, to_port);
	}
	return false;
}

bool VCClient::IsConnected()
{
	VCAutoMutex lock(m_cli_mutex);
	if (m_cli && m_cli->m_net)
	{
		return m_cli->m_net->IsConnected();
	}
	return false;
}

void VCClient::Disconnect()
{
	VCAutoMutex lock(m_cli_mutex);
	if (m_cli && m_cli->m_net)
	{
		if (m_cli->m_micon && m_cli->m_in)
		{
			m_cli->m_in->Cmd(VC_STOP);
			m_cli->m_micon = false;
		}
		return m_cli->m_net->Disconnect();
	}
}

bool VCClient::MicOn()
{
	VCAutoMutex lock(m_cli_mutex);
	if (m_cli && !m_cli->m_micon && m_cli->m_in && m_cli->m_net && m_cli->m_net->IsConnected())
	{
		m_cli->m_micon = (m_cli->m_in->Cmd(VC_START) == VC_OK);
		return m_cli->m_micon;
	}
	return false;
}

bool VCClient::IsMicOn()
{
	VCAutoMutex lock(m_cli_mutex);
	if (m_cli)
	{
		return m_cli->m_micon;
	}
	return false;
}

void VCClient::MicOff()
{
	VCAutoMutex lock(m_cli_mutex);
	if (m_cli && m_cli->m_micon && m_cli->m_in)
	{
		m_cli->m_in->Cmd(VC_STOP);
		m_cli->m_micon = false;
	}
}

void VCClient::SetChannel(char channel)
{
	VCAutoMutex lock(m_cli_mutex);
	if (m_cli && m_cli->m_in)
	{
		m_cli->m_in->Cmd(VC_SETCHANNEL, &channel, sizeof(char));
	}
}

void VCClient::SetVolume(unsigned char volume)
{
	VCAutoMutex lock(m_cli_mutex);
	if (m_cli && m_cli->m_out)
	{
		m_cli->m_out->SetVolume(volume);
	}
}

unsigned char VCClient::GetVolume()
{
	VCAutoMutex lock(m_cli_mutex);
	if (m_cli && m_cli->m_out)
	{
		return m_cli->m_out->GetVolume();
	}
	return 0;
}

bool VCClient::Init()
{
	if (m_cli)
	{
		return m_cli->Init();
	}
	return false;
}

